<?php
namespace ManagerX\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
    }

    public function login(){
        if(session('?user')){
            $this->redirect('Index/index');
        }else{
            $title = M('config')->where(array('name'=>'title'))->find();
            $version = M('config')->where(array('name'=>'ver'))->find();
            $url = M('config')->where(array('name'=>'url'))->find();
            $this->assign('url', $url['value']);
            $this->assign('title', $title['value']);
            $this->assign('version', $version['value']);
            $this->display('login');
        }
    }

    public function logout(){
        session(null);
        $this->redirect('User/login');
    }

    
    public function captcha(){
        $Verify = new \Think\Verify(array(
			'fontSize'    =>    20,    // 验证码字体大小
			'length'      =>    4,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
			'codeSet'     =>	'0123456789'
		));
		$Verify->entry();
    }

    public function user_list(){
        if(session('?user') && session('user.role') == 1){
            $this->display('user_list');
        }else{
            $this->error('未登录或无权限。', 'User/login', 3);
        }
    }

    public function user_edit(){
        if(session('?user') && session('user.role') == 1){
            $user = M('admin')->field('id, username, phone, role')->where(array('id'=>I('get.id')))->find();

            $this->assign('user', $user);
            $this->display('user_edit');
        }else{
            $this->error('未登录或无权限。', 'User/login', 3);
        }
    }

    public function user_add(){
        if(session('?user') && session('user.role') == 1){
            $this->display('user_add');
        }else{
            $this->error('未登录或无权限。', 'User/login', 3);
        }
    }

    public function user_edit_self(){
        if(session('?user')){
            $user = M('admin')->field('id, username, phone, role')->where(array('id'=>session('user.id')))->find();

            $this->assign('user', $user);
            $this->display('user_edit');
        }else{
            $this->error('未登录或无权限。', 'User/login', 3);
        }
    }
}